#### installing packages if not installed ####

list.of.packages <- c('tidyverse',
                      'ggplot2',
                      'lubridate',
                      'estimatr',
                      'ggthemes',
                      'readr',
                      'readxl',
                      'rdrobust',
                      'gridExtra')
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)

#### attaching libraries ####

suppressPackageStartupMessages(
  
  {
    
    library(tidyverse)
    library(ggplot2)
    library(lubridate)
    library(estimatr)    
    library(ggthemes)
    library(readr)
    library(readxl)
    library(rdrobust)
    library(gridExtra)
    
  }
  
)

#### loading in policing data for descriptive plots #### 

# Seattle
# 1) Terry Stops
# 2) Officer-initiated calls 

  # loading terry stop data 

load("data/terry.RData")

terry = terry %>% 
  mutate(blm = ifelse(date >= as.Date("2020-05-29"), 1, 0)) %>% 
  mutate(initiate_off = ifelse(call_type == "-" | call_type == "ONVIEW" | 
                                 call_type == "SCHEDULED EVENT (RECURRING)" | 
                                 call_type == "ALARM CALL (NOT POLICE ALARM)", 1, 0),
         initiate_civ = 
           ifelse(call_type == "911" | 
                    call_type == "TELEPHONE OTHER, NOT 911" | 
                    call_type == "TEXT MESSAGE", 1, 0))

  # loading 911 call data

load(file = "data/calldata.RData")

call = 
  call %>% 
  mutate(blm = ifelse(date >= as.Date("2020-05-29"), 1, 0)) %>% 
  mutate(initiate_off = ifelse(call_type == "-" | call_type == "ONVIEW" | 
                                 call_type == "SCHEDULED EVENT (RECURRING)" | 
                                 call_type == "ALARM CALL (NOT POLICE ALARM)" | 
                                 call_type == "PROACTIVE (OFFICER INITIATED)" | 
                                 call_type == "POLICE (VARDA) ALARM", 1, 0),
         initiate_civ = 
           ifelse(call_type == "911" | 
                    call_type == "TELEPHONE OTHER, NOT 911" | 
                    call_type == "TEXT MESSAGE" | 
                    call_type == "HISTORY CALL (RETRO)" | 
                    call_type == "IN PERSON COMPLAINT", 1, 0))

  # gen time data

call$time_arrived2 = parse_date_time(call$time_arrived, "%m/%d/%Y %H:%M:%S %p")
call$time_queued2 = parse_date_time(call$time_queued, "%m/%d/%Y %H:%M:%S %p")

call$time_arrived2 = 
  as.POSIXct(call$time_arrived, format="%m/%d/%Y %H:%M:%S %p", tz="UTC")
call$time_queued2 = 
  as.POSIXct(call$time_queued, format="%m/%d/%Y %H:%M:%S %p", tz="UTC")
call$response_time = (call$time_arrived2 - call$time_queued2) / 60
the_index = call$response_time < 0
call = call %>% filter(!the_index)

# Philly 
# 1) Pedestrian Stops 
# 2) Traffic Stops 

# loading in stop data 

load(file = "data/ph_stops_ped.RData")
load(file = "data/ph_stops_veh.RData")

# Austin
# 1) Traffic Stops 

# loading in traffic stop data 

load(file = "data/rpf.RData")

# Los Angeles
# 1) Pedestrian stops
# 2) Traffic Stops
# 3) Officer-Initiated Calls

# stops 

load("data/stopdata_la.RData")

# calls

load("data/calls_la.RData")

#### generating descriptive plot #### 

# alphabetical order
# 2 months before and after
# Austin
# Los Angeles
# Philly 
# Seattle 

# Austin (Traffic Stops)

rpf_ts = rpf %>% 
  mutate(count = 1) %>% 
  group_by(date) %>% 
  summarize(count = sum(count, na.rm = TRUE),
            blm = mean(blm, na.rm = TRUE)) %>% 
  filter(date >= as.Date("2020-03-23"))

rpf_ts$blmrv = 
  rpf_ts$date - min(rpf_ts$date[rpf_ts$blm == 1])
rpf_ts = rpf_ts %>% filter(blmrv <= 67)

descstops1 = rpf_ts %>% 
  ggplot() + 
  geom_point(aes(x = date, y = count),
             size = .3,
             alpha = .3) + 
  geom_smooth(aes(x = date, y = count, group = blm),
              se = FALSE,
              col = 'black',
              size = .3) + 
  geom_vline(xintercept = as.Date("2020-05-29"),
             linetype = 2,
             size = .3) + 
  labs(x = "Date", y = "Traffic Stops",
       title = "A. Traffic Stops (AUS)") + 
  theme_tufte(base_size = 7.5)

# Los Angeles (Pedestrian Stops) 

stops_ped_ts = stopdat1 %>% 
  filter(traffic_stop == 0) %>% 
  mutate(count = 1) %>% 
  group_by(date) %>% 
  summarize(count = sum(count, na.rm = TRUE),
            blm = mean(blm, na.rm = TRUE)) %>% 
  filter(date >= as.Date("2020-03-23"))

stops_ped_ts$blmrv = 
  stops_ped_ts$date - min(stops_ped_ts$date[stops_ped_ts$blm == 1])
stops_ped_ts = stops_ped_ts %>% filter(blmrv <= 67)

descstops2 = stops_ped_ts %>% 
  ggplot() + 
  geom_point(aes(x = date, y = count),
             size = .3,
             alpha = .3) + 
  geom_smooth(aes(x = date, y = count, group = blm),
              se = FALSE,
              col = 'black',
              size = .3) + 
  geom_vline(xintercept = as.Date("2020-05-28"),
             linetype = 2,
             size = .3) + 
  labs(x = "Date", y = "Pedestrian Stops",
       title = "B. Pedestrian Stops (LA)") + 
  theme_tufte(base_size = 7.5)

# Los Angeles (Traffic Stops) 

stops_veh_ts = stopdat1 %>% 
  filter(traffic_stop == 1) %>% 
  mutate(count = 1) %>% 
  group_by(date) %>% 
  summarize(count = sum(count, na.rm = TRUE),
            blm = mean(blm, na.rm = TRUE)) %>% 
  filter(date >= as.Date("2020-03-23"))

stops_veh_ts$blmrv = 
  stops_veh_ts$date - min(stops_veh_ts$date[stops_veh_ts$blm == 1])
stops_veh_ts = stops_veh_ts %>% filter(blmrv <= 67)

descstops3 = stops_veh_ts %>% 
  ggplot() + 
  geom_point(aes(x = date, y = count),
             size = .3,
             alpha = .3) + 
  geom_smooth(aes(x = date, y = count, group = blm),
              se = FALSE,
              col = 'black',
              size = .3) + 
  geom_vline(xintercept = as.Date("2020-05-28"),
             linetype = 2,
             size = .3) + 
  labs(x = "Date", y = "Traffic Stops",
       title = "C. Traffic Stops (LA)") + 
  theme_tufte(base_size = 7.5)

## Los Angeles (Officer-Initiated Calls) 
#
#calls_off_ts = lacallsf %>% 
#  mutate(count = 1) %>%
#  filter(off_call == 1) %>% 
#  group_by(date) %>% 
#  summarize(count = sum(count, na.rm = TRUE),
#            blm = mean(blm, na.rm = TRUE)) %>% 
#  filter(date >= as.Date("2020-03-23"))
#
#calls_off_ts$blmrv = 
#  calls_off_ts$date - min(calls_off_ts$date[calls_off_ts$blm == 1])
#calls_off_ts = calls_off_ts %>% filter(blmrv <= 67)
#
#descstops4 = 
#  calls_off_ts %>% 
#  filter(date >= as.Date("2019-01-01")) %>% 
#  ggplot() + 
#  geom_point(aes(x = date, y = count),
#             size = .3,
#             alpha = .3) + 
#  geom_smooth(aes(x = date, y = count, group = blm),
#              se = FALSE,
#              col = 'black',
#              size = .3) + 
#  geom_vline(xintercept = as.Date("2020-05-29"),
#             linetype = 2,
#             size = .3) + 
#  labs(x = "Date", y = "Officer-Initiated Calls",
#       title = "D. Officer-Initiated Calls (LA)") + 
#  theme_tufte(base_size = 7.5)
#
# Philly (Pedestrian Stops)

stops_ts = stops_ped %>% 
  mutate(count = 1) %>% 
  group_by(date) %>% 
  summarize(count = sum(count, na.rm = TRUE),
            blm = mean(blm, na.rm = TRUE)) %>% 
  filter(date >= as.Date("2020-03-23"))

stops_ts$blmrv = 
  stops_ts$date - min(stops_ts$date[stops_ts$blm == 1])
stops_ts = stops_ts %>% filter(blmrv <= 67)

descstops5 = stops_ts %>% 
  ggplot() + 
  geom_point(aes(x = date, y = count),
             size = .3,
             alpha = .3) + 
  geom_smooth(aes(x = date, y = count, group = blm),
              se = FALSE,
              col = 'black',
              size = .3) + 
  geom_vline(xintercept = as.Date("2020-05-29"),
             linetype = 2,
             size = .3) + 
  labs(x = "Date", y = "Pedestrian Stops",
       title = "D. Pedestrian Stops (PHL)") + 
  theme_tufte(base_size = 7.5)

# Philly (Traffic Stops)

stops_ts = stops_veh %>% 
  mutate(count = 1) %>% 
  group_by(date) %>% 
  summarize(count = sum(count, na.rm = TRUE),
            blm = mean(blm, na.rm = TRUE)) %>% 
  filter(date >= as.Date("2020-03-23"))

stops_ts$blmrv = 
  stops_ts$date - min(stops_ts$date[stops_ts$blm == 1])
stops_ts = stops_ts %>% filter(blmrv <= 67)

descstops6 = stops_ts %>% 
  ggplot() + 
  geom_point(aes(x = date, y = count),
             size = .3,
             alpha = .3) + 
  geom_smooth(aes(x = date, y = count, group = blm),
              se = FALSE,
              col = 'black',
              size = .3) + 
  geom_vline(xintercept = as.Date("2020-05-29"),
             linetype = 2,
             size = .3) + 
  labs(x = "Date", y = "Traffic Stops",
       title = "E. Traffic Stops (PHL)") + 
  theme_tufte(base_size = 7.5)

# seattle (terry stops)

terry_desc = terry %>% 
  filter(initiate_off == 1) %>% 
  mutate(count = 1) %>% 
  group_by(date) %>% 
  summarize(count = sum(count, na.rm = TRUE),
            blm = mean(blm)) %>% 
  filter(date >= as.Date("2020-03-23"))
terry_desc$blmrv = 
  terry_desc$date - min(terry_desc$date[terry_desc$blm == 1])
terry_desc = terry_desc %>% filter(blmrv <= 67)

descstops7 = terry_desc %>% 
  ggplot() + 
  geom_point(aes(x = date, y = count),
             size = .3,
             alpha = .3) + 
  geom_smooth(aes(x = date, y = count, group = blm),
              se = FALSE,
              col = 'black',
              size = .3) + 
  geom_vline(xintercept = as.Date("2020-05-29"),
             linetype = 2,
             size = .3) + 
  labs(x = "Date", y = "Terry Stops",
       title = "F. Terry Stops (SEA)") + 
  theme_tufte(base_size = 7.5)

## b) officer initiated calls 
#
#call_desc = call %>% 
#  filter(initiate_off == 1) %>% 
#  mutate(count = 1) %>% 
#  group_by(date) %>% 
#  summarize(count = sum(count),
#            blm = mean(blm)) %>% 
#  filter(date >= as.Date("2020-03-23"))
#call_desc$blmrv = 
#  call_desc$date - min(call_desc$date[call_desc$blm == 1])
#call_desc = call_desc %>% filter(blmrv <= 67)
#
#descstops8 = call_desc %>% 
#  ggplot() + 
#  geom_point(aes(x = date, y = count),
#             size = .3,
#             alpha= .3) + 
#  geom_smooth(aes(x = date, y = count, group = blm),
#              se = FALSE,
#              col = 'black',
#              size = .3) + 
#  geom_vline(xintercept = as.Date("2020-05-29"),
#             linetype = 2,
#             size = .3) + 
#  labs(x = "Date", y = "Officer-Intiated Calls",
#       title = "H. Officer-Initiated Calls (SEA)") + 
#  theme_tufte(base_size = 7.5)

desc_stops_grob = 
  arrangeGrob(descstops1, descstops2, descstops3,
            descstops5, descstops6, descstops7, ncol = 3)

ggsave(plot = desc_stops_grob, width = 8, height = 4, filename = "pics/figure1desc.png")
